﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fynd.Hermes.Web.Objects;
using FyndSharp.BusinessLogic;

namespace Fynd.Hermes.Web.Pages
{
    public partial class ChangePassword : RoleBasedPage
    {
        protected override IEnumerable<UserRole> AccreditedRoles
        {
            get
            {
                return new UserRole[] { UserRole.Guest, UserRole.NormalUser, UserRole.Administrator };
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonChange_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(TextBoxOldPassword.Text))
            {
                LabelMessage.Text = "修改密码前要输入旧密码先～";
                return;
            }

            if (String.IsNullOrEmpty(TextBoxPassword.Text))
            {
                LabelMessage.Text = "难道想改成空密码吗？那是绝对不行滴！";
                return;
            }

            if (String.IsNullOrEmpty(TextBoxPassword2.Text))
            {
                LabelMessage.Text = "新密码再输入一次比较好，不然会搞错的。";
                return;
            }

            if (false == TextBoxPassword.Text.Equals(TextBoxPassword2.Text, StringComparison.CurrentCulture))
            {
                LabelMessage.Text = "两次输入的新密码都不一样啊。";
                return;
            }



            UserInfomation theUser = Context.Items[Global.HttpContextKeyUser] as UserInfomation;
            UserLogic theUserLogic = new DefaultLogicFactory().CreateLogic<UserLogic>(new DefaultLogicUser(theUser));

            if (null == theUserLogic.FetchUserBy(theUser.LoginName, TextBoxOldPassword.Text))
            {
                LabelMessage.Text = "旧密码不对哦。";
            }
            else
            {
                if (theUserLogic.ChangePassword(theUser.LoginName, TextBoxOldPassword.Text, TextBoxPassword.Text))
                {
                    LabelMessage.Text = "修改成功！不过你需要马上退出重新登录了。";
                    Session.Abandon();
                    System.Web.Security.FormsAuthentication.SignOut();
                }
                else
                {
                    LabelMessage.Text = "纳尼，，，修改密码可耻地失败鸟。。。。";
                }
            }
        }
    }
}